package com.example.thread.demo.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

/**
 * @author zhy
 * @date 2020/5/20 2:49 下午
 * @description
 */
public class CompletableFutureTest2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("completableFuture 任务开始执行");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("completableFuture 任务结束执行");
            return "张海洋";
        });

        Executors.newSingleThreadExecutor().execute(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("主动设置completableFuture任务结果");
            completableFuture.complete("程序通事");
        });

        System.out.println("get：" + completableFuture.get());
        Thread.sleep(10000);
        System.out.println("get：" + completableFuture.get());

    }
}
